package com.zt.fbg.utils;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 响应数据
 *
 * @author carl
 * @since 1.0.0
 */
@Data
@ApiModel(value = "响应")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 编码：0表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：0表示成功，其他值表示失败")
    private Integer code = 0;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String message = "success";

    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;
    public Result() {
       this.data= (T) Collections.EMPTY_MAP;
    };
    public static <T>Result<T> ok(T data) {
        Result R = new Result();
        R.setData(data);
        return R;
    }
    public static <T>Result<T> ok() {
        Result R = new Result();
        return R;
    }
    public static <T>Result<T> with(T data) {
        Result R = new Result();
        R.setData(data);
        return R;
    }

    public static <T> Result<T> with(Integer code, String msg, T data) {
        Result R = new Result();
        R.setCode(code);
        R.setMessage(msg);
        R.setData(data);
        return R;
    }

    public static <T> Result<T> with(Integer code, String msg, T data,boolean isDebug,Exception e) {
        Result R = new Result();
        R.setCode(code);
        R.setMessage(msg);
        R.setData(data);
        return R;
    }



    public static <T> Result<T> error(Integer code, String msg) {

        return (Result<T>) with(code,msg, Collections.EMPTY_MAP);
    }





    public static <T> Result<T> error(Integer code, String msg,boolean isDebug,Exception e) {

        return (Result<T>) with(code,msg, Collections.EMPTY_MAP,isDebug,e);
    }





    public static Result withSuccess() {
        return new Result();
    }

    /**
     * 未登录返回结果
     */
    public static <T> Result<T> unauthorized(T data) {
        return  with(401, "暂未登录或token已经过期", data);
    }
    /**
     * 未授权返回结果
     */
    public static <T> Result<T> forbidden(T data) {
        return with(403,"没有相关权限", data);
    }
}
